﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Practices.Unity;
using Presentation.Shell.Interfaces;
using Microsoft.Practices.Prism.Regions;
using System.Diagnostics;

namespace VPL.Shell
{
    /// <summary>
    /// Interaction logic for MainMenu.xaml
    /// </summary>
    public partial class MainMenu : UserControl
    {
        public MainMenu(IUnityContainer container, IRegionManager regionManager)
        {
            InitializeComponent();

            this.Container = container;
            // Resolve all Presentation Modules
            this.ModuleList = container.ResolveAll<IPresentationModule>().ToList();
            this.RegionManager = regionManager;
            this.DataContext = this;
        }

        public List<IPresentationModule> ModuleList { get; set; }
        public IUnityContainer Container { get; set; }
        public IRegionManager RegionManager { get; set; }
        public IPresentationModule SelectedPresentationModule { get; set; }        

        private void MenuList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (MenuList.SelectedItem != null)
            {
                var page = ((IPresentationModule)MenuList.SelectedItem).LandingPage;
                // Added for navigating back to main menu.

                page.ViewExit += this.ChildViewExit;

                page.ParentView = this;
                RegionManager.Regions["MainRegion"].Add(page);
                RegionManager.Regions["MainRegion"].Activate(page);
            }
        }

        private void ChildViewExit()
        {
            this.RegionManager.Regions["MainRegion"].Activate(this);
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (MenuList.SelectedItem != null)
            {
                var page = ((IPresentationModule)MenuList.SelectedItem).LandingPage;
                // Added for navigating back to main menu.

                page.ViewExit += this.ChildViewExit;

                page.ParentView = this;
                RegionManager.Regions["MainRegion"].Add(page);
                RegionManager.Regions["MainRegion"].Activate(page);
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Process.Start(System.Windows.Forms.Application.ExecutablePath);
            Environment.Exit(0);
        }

        public string CurrentUser { get; set; }
    }
}
